package com.jframe.cfg.service.impl;


import java.util.ArrayList;
import java.util.List;

import com.jframe.cfg.model.Rolemenu;
import com.jframe.cfg.model.Sysmenu;
import com.jframe.cfg.service.RolemenuService;
import com.jframe.cfg.service.SysmenuService;
import com.jframe.sso.model.CurUser;
import com.jframe.sso.model.Role;
import com.jframe.sso.service.RoleService;
import com.jframe.sys.orm.service.impl.BaseServiceImpl;
import com.jframe.sys.utils.SpringUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Accrodion;


public class SysmenuServiceImpl extends BaseServiceImpl<Sysmenu> implements SysmenuService{

	@Override
	public List<Sysmenu> findTopSysmenu() {
		// TODO Auto-generated method stub
		String hql = "from Sysmenu where upId=? and active=? order by seq";
		return this.find(hql,new Object[]{1,1});
	}

	@Override
	public List<Sysmenu> findListByUpId(int upId) {
		// TODO Auto-generated method stub
		String hql = "from Sysmenu where upId=? and active=? order by seq";	
		return this.find(hql, new Object[]{upId,1});
	}

	@Override
	public List<Sysmenu> findListByUpIdAndGroupId(int upId, int groupId) {
		// TODO Auto-generated method stub
		if(upId==0){
			String hql = "from Sysmenu where upId=? order by seq";	
			return this.find(hql,upId);
		}else{
			String hql = "from Sysmenu where groupId=? and upId=? and active=? order by seq";	
			return this.find(hql, new Object[]{groupId,upId,1});
		}
	}

	@Override
	public List<Sysmenu> findListByGroupId(int groupId) {
		// TODO Auto-generated method stub
			String hql = "from Sysmenu where (groupId=? or id=?) and active=? order by seq";	
			return this.find(hql, new Object[]{groupId,1,1});
	} 

	@Override
	public List<Accrodion> findRoleAccrodions(CurUser user) {
		// TODO Auto-generated method stub
		RoleService roleService = (RoleService)SpringUtil.getBean("roleService");
		//SysmenuService sysmenuService = (SysmenuService)SpringUtil.getBean("sysmenuService");
		RolemenuService rolemenuService = (RolemenuService)SpringUtil.getBean("rolemenuService");
		//CurUser user = this.getCurUser();
		Role role = roleService.load(user.getRoleId());
		Rolemenu rolemenu = rolemenuService.load("roleId",role.getId());
		String[] menuIds = StringUtil.null2Space(rolemenu.getMenuIds()).split(",");
		List<Sysmenu> topls = this.findTopSysmenu();
		List<Accrodion> ls = new ArrayList<Accrodion>();
		for(Sysmenu menu:topls){
			boolean flag = false;
			for(String menuId:menuIds ){
				if(menuId.equals(""+menu.getId())){
					flag=true;
				}
			}
			if(flag){
				Accrodion accrodion = new Accrodion();
				accrodion.setId(""+menu.getId());
				accrodion.setTitle(menu.getText());
				accrodion.setIconCls(menu.getIconCls());
				ls.add(accrodion);
			}
		}
		return ls;
	}

	@Override
	public Sysmenu getTopSysmenuByTitleAndGroup(String title,int groupId) {
		// TODO Auto-generated method stub
		String hql = "from Sysmenu where text=? and groupId=? and active=? and upId=? order by seq";	
		return this.find(hql, new Object[]{title,groupId,1,1}).get(0);
	}
	
	
	
	

}
